﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class CachDungDAL
    {
        PhongMachTuDBDataContext db;
        public List<CACHDUNG> LayDSCachDung()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.CACHDUNGs
                    select s;
            return q.ToList();
        }
        public List<CACHDUNG> timKiem(string maCD, string tenCD)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.CACHDUNGs
                    where s.MaCachDung.Contains(maCD) || s.TenCachDung.Contains(tenCD)
                    select s;
            return q.ToList();
        }
        public bool ThemCachDung(CACHDUNG s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                CACHDUNG r = new CACHDUNG();
                r.TenCachDung = s.TenCachDung;
                r.MaCachDung = s.MaCachDung;
                db.CACHDUNGs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaCachDung(CACHDUNG s)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                CACHDUNG r = db.CACHDUNGs.Single(id => id.MaCachDung == s.MaCachDung);
                r.TenCachDung = s.TenCachDung;
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaCachDung(string MaCachDung)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.CACHDUNGs
                             where d.MaCachDung == MaCachDung
                             select d;
                if (delete.Count() > 0)
                {
                    db.CACHDUNGs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
